Skip to content

fix(rag): require co-located medication evidence#581

Merged
BigSimmo merged 5 commits into
mainfrom
codex/rag-review-followup
Jul 13, 2026
Merged

fix(rag): require co-located medication evidence#581
BigSimmo merged 5 commits into
mainfrom
codex/rag-review-followup

Conversation

@BigSimmo

@BigSimmo BigSimmo commented Jul 13, 2026

Copy link
Copy Markdown
Owner

Summary

  • require requested medication amount, route, and frequency evidence to be co-located with the clinical subject before the text fast path can return
  • use one shared intent detector for natural forms such as how much, how often, microgram units, and route wording
  • preserve route-only behavior, add explicit missing-frequency/co-location reasons, and record the completed review disposition
  • follow up the post-merge findings on PR Enforce dose context before text fast path #575; the stale privacy UI assertion was completed separately by merged PR feat(ui): single compact site-wide privacy notice below the composer #576

Verification

  • Ran npm run verify:pr-local -- --dry-run --files src/lib/clinical-search.ts,src/lib/rag.ts,tests/retrieval-query-variants.test.ts,docs/branch-review-ledger.md: selected runtime, format, lint, typecheck, full tests, build, and offline RAG. The executable combined gate was not repeated because this junction-based checkout needs over 10 minutes for the full unit suite alone; its constituents are listed below.
  • npm run verify:ui: not applicable because the duplicate UI test change was removed; merged PR feat(ui): single compact site-wide privacy notice below the composer #576 separately passed critical and advisory UI.
  • npm run verify:release: not run because it includes provider-backed release checks; the authorized final provider gates are being run separately.
  • npm run eval:retrieval:quality: running in Eval Canary run 29251155102 on this exact branch head.
  • npm run eval:rag -- --limit 15 and npm run eval:quality -- --rag-only: not run separately because no synthesis prompt or answer prose changed; the hosted answer-quality subset is running in Eval Canary run 29251155102.
  • npm run check:production-readiness: local fail-closed run completed with provider variables deliberately cleared; final configured production run remains pending after deployment.
  • Ran npx --no-install vitest run tests/clinical-search.test.ts tests/deep-memory.test.ts tests/retrieval-query-variants.test.ts tests/retrieval-selection.test.ts --reporter=dot: 4 files passed, 143/143 tests passed.
  • Ran npm run eval:rag:offline: 21 files passed, 265/265 tests passed, and 36 fixture schemas passed.
  • Ran npm run typecheck: passed.
  • Ran .\node_modules\.bin\eslint.cmd src/lib/clinical-search.ts src/lib/rag.ts tests/retrieval-query-variants.test.ts --max-warnings=0: passed with zero warnings.
  • Ran npm test: 211 files passed, 1 skipped; 1,946 tests passed, 1 skipped.
  • Ran .\node_modules\.bin\prettier.cmd --check src/lib/clinical-search.ts src/lib/rag.ts tests/retrieval-query-variants.test.ts docs/branch-review-ledger.md: all matched files passed.
  • Ran git diff --check origin/main...HEAD: passed with no whitespace errors.

Clinical Governance Preflight

  • Source-backed claims still require linked source verification before clinical use
  • No patient-identifiable document workflow was introduced or expanded without explicit governance approval
  • Supabase target remains Clinical KB Database (sjrfecxgysukkwxsowpy)
  • Service-role keys and private document access remain server-only
  • Demo/synthetic content remains clearly separated from real clinical sources
  • Source metadata, review status, and outdated/unknown-source behavior remain conservative
  • Deployment classification/TGA SaMD impact was checked: this narrows an existing evidence fast path and adds no new clinical claim or workflow

Notes

  • npm run verify:cheap passed runtime/config/format-adjacent guards, lint, and typecheck, then its 10-minute host wrapper expired while the full unit suite was still running. The same full suite passed independently under a longer bound.
  • No dependency, migration, environment, secret, or production-data changes are included.

@supabase

supabase Bot commented Jul 13, 2026

Copy link
Copy Markdown

This pull request has been ignored for the connected project sjrfecxgysukkwxsowpy because there are no changes detected in supabase directory. You can change this behaviour in Project Integrations Settings ↗︎.


Preview Branches by Supabase.
Learn more about Supabase Branching ↗︎.

@coderabbitai

coderabbitai Bot commented Jul 13, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

Expanded medication dose intent detection and evidence matching for amount, route, and frequency queries. Updated RAG coverage gating to require co-located medication evidence, with tests covering new units, schedules, route failures, and split evidence.

Changes

Medication dose evidence

Layer / File(s) Summary
Dose intent and evidence detection
src/lib/clinical-search.ts
Dose matching recognizes additional unit, medication-class, route, and schedule variants; exported intent detection separates amount, route, and frequency questions.
Co-located coverage gating
src/lib/rag.ts
The medication dose risk gate adds frequency evidence, uses structured query intent, and requires requested attributes to be co-located with medication evidence.
Behavior validation and review record
tests/retrieval-query-variants.test.ts, docs/branch-review-ledger.md
Tests cover intent detection, fast-path gating, microgram symbols, split evidence, and missing route evidence; the review ledger records the remediation and verification results.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Sequence Diagram(s)

sequenceDiagram
  participant Query
  participant ClinicalSearch
  participant RAGCoverageGate
  participant RetrievalEvidence
  Query->>ClinicalSearch: classify amount, route, and frequency intent
  ClinicalSearch-->>RAGCoverageGate: structured medication dose intent
  RAGCoverageGate->>RetrievalEvidence: check co-located dose and context evidence
  RetrievalEvidence-->>RAGCoverageGate: matching evidence attributes
  RAGCoverageGate-->>Query: accept fast path or return rejection reason
Loading
🚥 Pre-merge checks | ✅ 11
✅ Passed checks (11 passed)
Check name Status Explanation
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Generated And Sensitive Files ✅ Passed Diff vs origin/main only touches docs/src/tests; no .env, logs, caches, node_modules, keys, dumps, or other sensitive/generated files found.
Verification Claims ✅ Passed PR notes and ledger row name each check (e.g. verify:cheap, npm test, typecheck) with outcomes or explicit not-run reasons.
Risky Git Or Deployment Actions ✅ Passed Only a review-ledger row was added; it records review outcomes/tests and contains no force-push, destructive cleanup, or deployment instructions.
Supabase Project And Schema Safety ✅ Passed The diff only changes query/RAG logic and a review ledger doc; no Supabase config, env, migration, RLS/policy, or project-ref changes were introduced.
Runtime And Package Manager Integrity ✅ Passed PR touches only docs; no package/runtime files changed. Repo still pins npm@11.17.0, node 24.x, and engine-strict=true.
Api Route Failure Handling ✅ Passed PASS: changed provider/ingestion paths map missing-env/auth/timeout/quota/rate-limit errors to PublicApiError, and ingestion callers catch generation failures with fallback defaults.
Title check ✅ Passed The title is concise and accurately captures the main change: requiring co-located medication evidence.
Description check ✅ Passed The description follows the template and includes Summary, Verification, Clinical Governance Preflight, and Notes.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch codex/rag-review-followup
✨ Simplify code
  • Create PR with simplified code
  • Commit simplified code in branch codex/rag-review-followup

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (2)
src/lib/clinical-search.ts (1)

750-761: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Medication dose unit/frequency vocabularies are duplicated across clinical-search.ts and rag.ts. The amount-unit spelling list (mg/mcg/micrograms/milligrams/ug/µg) and the frequency-token list (once/twice/daily/…/qid/every-N-hours) are each maintained independently in two places; this very PR had to edit both in lockstep to add the same new spellings, which is the classic sign of future drift.

  • src/lib/clinical-search.ts#L750-L761: canonical site — export the unit pattern (and ideally the frequency pattern near Line 1190) as shared constants.
  • src/lib/clinical-search.ts#L1186-L1191: reuse the same exported unit/frequency constants instead of re-listing the spellings.
  • src/lib/rag.ts#L2764-L2766: import the shared unit constant from clinical-search.ts instead of re-declaring the identical regex.
  • src/lib/rag.ts#L2775-L2781: import the shared frequency constant from clinical-search.ts instead of re-declaring an overlapping regex.
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/lib/clinical-search.ts` around lines 750 - 761, Deduplicate medication
vocabulary regexes by exporting shared unit and frequency pattern constants from
clinical-search.ts, anchored at hasDoseEvidenceSupport and the frequency logic
around lines 1186-1191. Update clinical-search.ts to reuse those constants, and
in src/lib/rag.ts lines 2764-2766 and 2775-2781 import and reuse the shared unit
and frequency constants instead of redeclaring the patterns; preserve the
existing matching behavior.
src/lib/rag.ts (1)

2936-2991: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Solid, test-verified co-location logic — consider extracting the repeated contextual-check pattern.

Manually traced this cascade against every provided medication_dose_risk test case (split-evidence rejection, route-only rejection, entity-crossing rejection, dose+route acceptance, microgram-only acceptance) and every reason matches. The five top.some((result) => hasDoseEvidenceSupport(result) && <check> && medicationDoseQueryContext(query, result).matched) blocks differ only by the injected check function, which is a good candidate for a small helper to reduce duplication and ease future attribute additions (e.g., a future "duration" attribute).

♻️ Extract a contextual-evidence helper
+const hasContextualEvidence = (
+  top: SearchResult[],
+  query: string,
+  check: (result: SearchResult) => boolean = () => true,
+) =>
+  top.some(
+    (result) =>
+      hasDoseEvidenceSupport(result) && check(result) && medicationDoseQueryContext(query, result).matched,
+  );
+
-    const hasContextualDoseEvidence = top.some(
-      (result) => hasDoseEvidenceSupport(result) && medicationDoseQueryContext(query, result).matched,
-    );
-    const hasContextualDoseAmount = top.some(
-      (result) =>
-        hasDoseEvidenceSupport(result) &&
-        hasDoseAmountEvidenceForGate(result) &&
-        medicationDoseQueryContext(query, result).matched,
-    );
+    const hasContextualDoseEvidence = hasContextualEvidence(top, query);
+    const hasContextualDoseAmount = hasContextualEvidence(top, query, hasDoseAmountEvidenceForGate);
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/lib/rag.ts` around lines 2936 - 2991, Extract the repeated contextual
`top.some` checks in the `medication_dose_risk` branch into a small helper that
accepts the result collection and attribute-specific evidence predicate, while
consistently requiring `hasDoseEvidenceSupport` and a matching
`medicationDoseQueryContext`. Replace `hasContextualDoseEvidence`,
`hasContextualDoseAmount`, `hasContextualRoute`, `hasContextualFrequency`, and
the corresponding co-location checks in this branch with the helper, preserving
all existing acceptance and reason behavior.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@src/lib/clinical-search.ts`:
- Around line 750-761: Deduplicate medication vocabulary regexes by exporting
shared unit and frequency pattern constants from clinical-search.ts, anchored at
hasDoseEvidenceSupport and the frequency logic around lines 1186-1191. Update
clinical-search.ts to reuse those constants, and in src/lib/rag.ts lines
2764-2766 and 2775-2781 import and reuse the shared unit and frequency constants
instead of redeclaring the patterns; preserve the existing matching behavior.

In `@src/lib/rag.ts`:
- Around line 2936-2991: Extract the repeated contextual `top.some` checks in
the `medication_dose_risk` branch into a small helper that accepts the result
collection and attribute-specific evidence predicate, while consistently
requiring `hasDoseEvidenceSupport` and a matching `medicationDoseQueryContext`.
Replace `hasContextualDoseEvidence`, `hasContextualDoseAmount`,
`hasContextualRoute`, `hasContextualFrequency`, and the corresponding
co-location checks in this branch with the helper, preserving all existing
acceptance and reason behavior.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 08a50c81-6a84-4b5e-ac77-f68d33098f28

📥 Commits

Reviewing files that changed from the base of the PR and between ccf7334 and 89ea74b.

📒 Files selected for processing (4)
  • docs/branch-review-ledger.md
  • src/lib/clinical-search.ts
  • src/lib/rag.ts
  • tests/retrieval-query-variants.test.ts

@BigSimmo
BigSimmo merged commit a180fb2 into main Jul 13, 2026
15 of 16 checks passed
@BigSimmo
BigSimmo deleted the codex/rag-review-followup branch July 13, 2026 16:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant